<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 发送短信
 *
 * @author      Eden
 * @datetime    16/7/16 19:19
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Util_Sms
{
    private $appkey;
    private $template;

    const SMS_INTERFACE_URL = 'http://sms-api.luosimao.com/v1/send.json';

    /**
     * Service config
     * @param $config
     */
    public function __construct(Array $config)
    {
        if (empty($config['template'])) {
            return false;
        }

        foreach ($config as $key => $val) {
            $this->$key = $val;
        }
    }

    /**
     * sms interface
     * @param $appkey
     * @param $params
     * @return mixed
     */
    public function _send_message($params)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, self::SMS_INTERFACE_URL);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:key-' . $this->appkey);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

        $res = curl_exec($ch);
        curl_close($ch);

        return $res;
    }

    /**
     * 获取LUOSIMAO配置
     * @return array|bool|null
     */
    private function getAppKey()
    {
        $ci = & get_instance();
        $query = $ci->db->get_where('setting', array(
            'use' => 'system',
            'key' => 'service'
        ));
        if ($res = $query->row_array()) {
            try {
                $res = json_decode($res['value'], true);
                return !empty($res['luosimao']) ? value($res, 'luosimao') : false;
            } catch(Exception $e) {}
        }
        return false;
    }

    /**
     * 发送短信
     *
     * @param string $mobile
     * @param array $data
     * @return bool|int
     */
    public function send ($mobile = '', $data = array())
    {

        $appkey = $this->getAppKey();
        if (empty($appkey)) {
            return false;
        }
        $this->appkey = $appkey;

        foreach ($data as $key => $val) {
            $this->template = str_replace($key, $val, $this->template);
        }

        $res = $this->_send_message(array(
            'mobile'    => $mobile,
            'message'   => $this->template
        ));

        $smsResult = json_decode($res, true);
        return ((isset($smsResult['error']) && $smsResult['error'] == 0)
            && (isset($smsResult['msg']) && $smsResult['msg'] == 'ok')) ? true : false;
    }

    /**
     * 文字短信
     * @param string $mobile
     * @param string $tpl
     * @return bool
     */
    public function word ($mobile = '', $tpl = '')
    {

        $appkey = $this->getAppKey();
        if (empty($appkey)) {
            return false;
        }
        $this->appkey = $appkey;

        $this->template = $tpl;

        $res = $this->_send_message(array(
            'mobile'    => $mobile,
            'message'   => $this->template
        ));

        $smsResult = json_decode($res, true);
        return ((isset($smsResult['error']) && $smsResult['error'] == 0)
            && (isset($smsResult['msg']) && $smsResult['msg'] == 'ok')) ? true : false;
    }
}
